package com.example.demo.model.enums;

import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public enum UserTypeEnum {
    TEACHER("教师"),
    STUDENT("学生"),
    ADMIN("管理员"),
    UNKNOWN("未知类型"); // 新增的默认枚举值

    private final String description;

    UserTypeEnum(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 安全转换方法（处理null或无效值）
     * @param value 数据库存储的字符串值
     * @return 对应的枚举值，默认返回STUDENT
     */
    public static UserTypeEnum fromValue(String value) {
        if (StringUtils.isBlank(value)) {
            return STUDENT;
        }
        try {
            return valueOf(value.toUpperCase().trim());
        } catch (IllegalArgumentException e) {
            return STUDENT; // 或返回UNKNOWN根据业务需求
        }
    }

    /**
     * 用于MyBatis的类型转换器
     */
    public static class TypeHandler extends org.apache.ibatis.type.EnumTypeHandler<UserTypeEnum> {
        public TypeHandler() {
            super(UserTypeEnum.class);
        }

        @Override
        public UserTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
            return fromValue(rs.getString(columnName));
        }
    }
}